<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="合作伙伴名称" prop="partnerName">
      <el-input v-model="dataForm.partnerName" placeholder="合作伙伴名称"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴英文名称" prop="partnerNameEn">
      <el-input v-model="dataForm.partnerNameEn" placeholder="合作伙伴英文名称"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴log" prop="partnerLogo">
      <el-input v-model="dataForm.partnerLogo" placeholder="合作伙伴log"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴电话号码" prop="partnerPhone">
      <el-input v-model="dataForm.partnerPhone" placeholder="合作伙伴电话号码"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴地址" prop="partnerUrl">
      <el-input v-model="dataForm.partnerUrl" placeholder="合作伙伴地址"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴类型 0：国内 1：国外" prop="partnerType">
      <el-input v-model="dataForm.partnerType" placeholder="合作伙伴类型 0：国内 1：国外"></el-input>
    </el-form-item>
    <el-form-item label="合作伙伴备注" prop="partnerRemark">
      <el-input v-model="dataForm.partnerRemark" placeholder="合作伙伴备注"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updaateTime">
      <el-input v-model="dataForm.updaateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="状态 0 ：可用 1：不可用" prop="partnerStatus">
      <el-input v-model="dataForm.partnerStatus" placeholder="状态 0 ：可用 1：不可用"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0；存在 1：已删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除 0；存在 1：已删除"></el-input>
    </el-form-item>
    <el-form-item label="排序码" prop="partnerSort">
      <el-input v-model="dataForm.partnerSort" placeholder="排序码"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          partnerId: 0,
          partnerName: '',
          partnerNameEn: '',
          partnerLogo: '',
          partnerPhone: '',
          partnerUrl: '',
          partnerType: '',
          partnerRemark: '',
          createTime: '',
          updaateTime: '',
          partnerStatus: '',
          delFlag: '',
          partnerSort: '',
          createUser: ''
        },
        dataRule: {
          partnerName: [
            { required: true, message: '合作伙伴名称不能为空', trigger: 'blur' }
          ],
          partnerNameEn: [
            { required: true, message: '合作伙伴英文名称不能为空', trigger: 'blur' }
          ],
          partnerLogo: [
            { required: true, message: '合作伙伴log不能为空', trigger: 'blur' }
          ],
          partnerPhone: [
            { required: true, message: '合作伙伴电话号码不能为空', trigger: 'blur' }
          ],
          partnerUrl: [
            { required: true, message: '合作伙伴地址不能为空', trigger: 'blur' }
          ],
          partnerType: [
            { required: true, message: '合作伙伴类型 0：国内 1：国外不能为空', trigger: 'blur' }
          ],
          partnerRemark: [
            { required: true, message: '合作伙伴备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updaateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          partnerStatus: [
            { required: true, message: '状态 0 ：可用 1：不可用不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除 0；存在 1：已删除不能为空', trigger: 'blur' }
          ],
          partnerSort: [
            { required: true, message: '排序码不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.partnerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.partnerId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webpartner/info/${this.dataForm.partnerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.partnerName = data.webPartner.partnerName
                this.dataForm.partnerNameEn = data.webPartner.partnerNameEn
                this.dataForm.partnerLogo = data.webPartner.partnerLogo
                this.dataForm.partnerPhone = data.webPartner.partnerPhone
                this.dataForm.partnerUrl = data.webPartner.partnerUrl
                this.dataForm.partnerType = data.webPartner.partnerType
                this.dataForm.partnerRemark = data.webPartner.partnerRemark
                this.dataForm.createTime = data.webPartner.createTime
                this.dataForm.updaateTime = data.webPartner.updaateTime
                this.dataForm.partnerStatus = data.webPartner.partnerStatus
                this.dataForm.delFlag = data.webPartner.delFlag
                this.dataForm.partnerSort = data.webPartner.partnerSort
                this.dataForm.createUser = data.webPartner.createUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webpartner/${!this.dataForm.partnerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'partnerId': this.dataForm.partnerId || undefined,
                'partnerName': this.dataForm.partnerName,
                'partnerNameEn': this.dataForm.partnerNameEn,
                'partnerLogo': this.dataForm.partnerLogo,
                'partnerPhone': this.dataForm.partnerPhone,
                'partnerUrl': this.dataForm.partnerUrl,
                'partnerType': this.dataForm.partnerType,
                'partnerRemark': this.dataForm.partnerRemark,
                'createTime': this.dataForm.createTime,
                'updaateTime': this.dataForm.updaateTime,
                'partnerStatus': this.dataForm.partnerStatus,
                'delFlag': this.dataForm.delFlag,
                'partnerSort': this.dataForm.partnerSort,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
